package com.ayuku.retail.ui.stock;

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.FormDialogBasic;
import com.ayuku.commons.Util;
import com.ayuku.commons.UtilPojo;
import com.ayuku.retail.business.stock.StockImpl;
import com.ayuku.retail.persistence.model.Stock;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FormStock extends FormDialogBasic {
	private CompStock cmp;
	private String barcode;
	private String name;
	private String categoryCod;
	private String satuan;
	private Stock pjo;

	public FormStock(final Shell parent, final int style, final int mode) {
		super(parent, style, mode);
	}

	public void getPanelContent(final Group grpData) {
		cmp = new CompStock(grpData, SWT.NONE);
	}

	@Override
	public void additionalProses() {
		getDialogShell().setText("Add/Edit Customers");
		getDialogShell().setSize(388, 430);
		try {
			// Util.populateCmbGroup(cmpUser.cmbGroup);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Util.setCenter(getDialogShell());
	}

	@Override
	public void clearContols() {
		cmp.txtBarcode.setText("");
		cmp.txtName.setText("");
		cmp.txtUnit.setText("");
		cmp.txtCategory.setText("");
		cmp.txtReorderPoint.setValue(BigDecimal.valueOf(0));
		cmp.txtSellingPrice.setValue(BigDecimal.valueOf(0));
		cmp.txtUnit.setText("");
		cmp.txtPrice.setValue(BigDecimal.valueOf(0.0));
	}

	@Override
	protected void doLoadAdd(String key) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doLoad(String key) throws Exception {

		pjo = (Stock) AyukuDB.getPersist().readByPrimaryKey(
				Stock.class, new Object[] { key });

		cmp.txtBarcode.setText(Util.nullToString(pjo.getBarcode()));
		cmp.txtName.setText(Util.nullToString(pjo.getName()));
		cmp.txtUnit.setText(Util.nullToString(pjo.getUnit()));		
		cmp.txtCategory.setText(Util.nullToString(pjo.getCategoryCode()));
		cmp.txtReorderPoint.setValue(pjo.getReorderPoint());
		cmp.txtSellingPrice.setValue(pjo.getSellingPrice());
		cmp.txtPrice.setValue(pjo.getPrice());
		cmp.txtQtyOnHand.setValue(pjo.getQtyOnHand());
		cmp.txtDisc.setValue(pjo.getDiscount());
		cmp.chkBisaDiedit.setSelection(pjo.getIsHjBisaEdit()==null?false:pjo.getIsHjBisaEdit());
	}

	@Override
	public boolean doValidate() {
		barcode = cmp.txtBarcode.getText();
		if (Util.isEmpty(barcode)) {
			Util.showMsgBox("Barcode harus dilengkapi!", 1,
					getDialogShell());
			cmp.txtBarcode.setFocus();
			return false;
		}

		name = cmp.txtName.getText();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama barang harus dilengkapi!", 1, getDialogShell());
			cmp.txtName.setFocus();
			return false;
		}

		satuan = cmp.txtUnit.getText();
		if (Util.isEmpty(satuan)) {
			Util.showMsgBox("satuan harus dilengkapi!", 1, getDialogShell());
			cmp.txtUnit.setFocus();
			return false;
		}
		
		categoryCod = cmp.txtCategory.getText();
		if (Util.isEmpty(categoryCod)) {
			Util.showMsgBox("Category harus dilengkapi!", 1, getDialogShell());
			cmp.txtCategory.setFocus();
			return false;
		}

		return true;
	}

	@Override
	public void enabledControl(final boolean otherField, final boolean keyField) {
		cmp.txtBarcode.setEditable(keyField);
		cmp.txtName.setEditable(otherField);
		cmp.txtUnit.setEditable(otherField);
		cmp.txtCategory.setEditable(otherField);
		cmp.txtReorderPoint.setEnabled(otherField);
		cmp.txtSellingPrice.setEnabled(otherField);
		cmp.txtUnit.setEditable(otherField);
		cmp.txtPrice.setEnabled(otherField);
		cmp.txtDisc.setEnabled(otherField);
		cmp.txtQtyOnHand.setEnabled(otherField);
	}

	@Override
	public int doMainInsert() throws Exception {
		pjo = new Stock();
		setPojoWithData();
		return StockImpl.add(pjo);
	}

	@Override
	public int doMainDelete() throws Exception {
		return StockImpl.delete(pjo.getIdx()+"");
	}

	@Override
	public int doMainUpdate() throws Exception {
		setPojoWithData();
		return StockImpl.update(pjo);

	}

	@Override
	protected void setPojoWithData() throws Exception {
		pjo.setBarcode(barcode);
		pjo.setName(name);
		pjo.setCategoryCode(categoryCod);
		pjo.setUnit(cmp.txtUnit.getText());
		pjo.setActive("1");
		pjo.setDiscount(cmp.txtDisc.getValue());
		pjo.setMaxQty(BigDecimal.valueOf(1000));
		pjo.setReorderPoint(cmp.txtReorderPoint.getValue());
		pjo.setQtyOnHand(cmp.txtQtyOnHand.getValue());
		pjo.setPrice(cmp.txtPrice.getValue());
		pjo.setSellingPrice(cmp.txtSellingPrice.getValue());
		pjo.setIsHjBisaEdit(cmp.chkBisaDiedit.getSelection());
		UtilPojo.setDateAndUser(pjo);
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

}
